package com.vay.de.dang.online.ui.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.databinding.ActivityFeedbackBinding;
import com.vay.de.dang.online.ui.WithBack1Activity;
import com.vay.de.dang.online.ui.apply.TakeImageActivity;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.view.DialogSelectImage;
import com.vay.de.dang.online.web.BaseApi;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.web.ReqResult;

import java.util.ArrayList;
import java.util.List;

import cc.taylorzhang.singleclick.SingleClickUtil;
import droidninja.filepicker.utils.ContentUriUtils;

public class FeedbackActivity extends WithBack1Activity {

    public static final ArrayList<Uri> selectFile = new ArrayList<>();

    private ActivityFeedbackBinding binding;
    private List<String> paths = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFeedbackBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ((TextView) findViewById(R.id.title_tv)).setText(R.string.title_feedback);
        findViewById(R.id.bnt_back_home).setOnClickListener(view -> finish());

        SingleClickUtil.onSingleClick(binding.bntSubmit, view -> {

            String str = binding.edit.getText().toString();
            if (VnUtil.isEmpty(str)) {
                showHint(R.string.label_feedback_l5);
                return;
            }

            BaseApi.uploadFeedback(str, paths, new ReqCallback(mActivity) {
                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        showHint(R.string.label_feedback_ok);
                        finish();
                    }
                }
            }.showDialog(true));
        });

        createIcon(null);
    }

    private void createIcon(String path) {

        if (!VnUtil.isEmpty(path)) {
            paths.add(path);
        }

        updateImageLayout();
    }

    private void updateImageLayout() {
        int num = 3;
        int p = VnApp.getDimension(R.dimen.x16);
        int p2 = VnApp.getDimension(R.dimen.x12);
        int w = (VnApp.getScreenWidth() - p * 2 - (num - 1) * p2) / num;
        binding.imageLayout.removeAllViews();

        for (int index = 0; index < paths.size(); index++) {
            final String str = paths.get(index);
            View iv = getLayoutInflater().inflate(R.layout.view_image, null);
            ImageView icon = iv.findViewById(R.id.iv_image);
            ImageView del = iv.findViewById(R.id.bnt_delete);
            Bitmap src = BitmapFactory.decodeFile(str);
            icon.setImageBitmap(src);
            icon.setScaleType(ImageView.ScaleType.CENTER_CROP);

            del.setOnClickListener(v -> {
                paths.remove(str);
                updateImageLayout();
            });
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, w);
            lp.leftMargin = index > 0 ? p2 : 0;
            binding.imageLayout.addView(iv, lp);
        }

        if (paths.size() < 3) {
            ImageView icon = new ImageView(mActivity);
            icon.setImageResource(R.mipmap.ic_camera_upload);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, w);
            lp.leftMargin = paths.size() > 0 ? p2 : 0;
            binding.imageLayout.addView(icon, lp);
            icon.setOnClickListener(view -> {
                new DialogSelectImage(mActivity).showSelector(type -> {
                    if (type == 1) {
                        Intent intent = new Intent();
                        intent.setClass(mActivity, TakeImageActivity.class);
                        intent.putExtra("fromType", 2);
                        startActivityForResult(intent, 3000);
                    } else {
                        try {
                            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                            photoPickerIntent.setType("image/*");
                            startActivityForResult(photoPickerIntent, 4100);
                        } catch (Exception e) {
                            e.printStackTrace();
                            showHint("Sorry, Open album failed!");
                        }
                    }
                });
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;

        // 图片
        if (requestCode == 3000) {
            createIcon(findString(data, "_out_path"));
        }
        // 系统相册
        else if (requestCode == 4100) {
            try {
                String path = ContentUriUtils.INSTANCE.getFilePath(this, data.getData());
                if (path != null) {
                    createIcon(path);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
